package com.itdct.onflow.system.log.global

import com.itdct.onflow.system.log.entity.SysApiLogEntity
import java.util.*
import java.util.concurrent.ConcurrentLinkedDeque

/**
 * @author Zhouwx
 * @date 2025/10/23 16:12:10
 * @version 1.0
 * @description
 */
object SysApiLogList {
    val apiLogs = ConcurrentLinkedDeque<SysApiLogEntity>()
    fun add(apiLog: SysApiLogEntity) {
        apiLog.createTime = Date()
        apiLog.updateTime = Date()
        apiLogs.add(apiLog)
    }

    /**
     * 批量取出当前所有日志项，用于批量入库
     * 此方法保证了在批量转移过程中的线程安全
     */
    fun drainToCollection(): MutableList<SysApiLogEntity> {
        val tempList = mutableListOf<SysApiLogEntity>()
        // 循环取出直到队列为空，pollFirst()是线程安全的
        while (true) {
            val log = apiLogs.pollFirst()
            if (log == null) break
            tempList.add(log)
        }
        return tempList
    }
}